<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery" icon="el-icon-search" size="small">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" size="small" v-on:click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="tableData" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="realName" label="姓名" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" :formatter="formatState" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="电话">
      </el-table-column>
      <el-table-column prop="email" label="邮箱">
      </el-table-column>
      <el-table-column prop="inputTime" label="注册时间">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="edit( scope.row)" icon="el-icon-edit" type="primary">编辑</el-button>
          <el-button type="danger" size="small" @click="del( scope.row)" icon="el-icon-close">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0"
                 icon="el-icon-close">批量删除
      </el-button>
      <!--			<el-pagination layout="prev, pager, next"-->
      <!--                     @current-change="selectPage" :page-size="20" :total="total" style="float:right;">-->
      <!--			</el-pagination>-->

      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :current-page="pageNo"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     background
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="addForm.realName" auto-complete="off"/>
        </el-form-item>

        <el-form-item label="电话号码">
          <el-input v-model="addForm.tel" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="addForm.email" auto-complete="off"/>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="0">正常</el-radio>
            <el-radio class="radio" :label="1">锁定</el-radio>
            <el-radio class="radio" :label="2">注销</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="员工类型">
          <el-radio-group v-model="addForm.type">
            <el-radio class="radio" :label="1">平台普通员工</el-radio>
            <el-radio class="radio" :label="2">平台客服人员</el-radio>
            <el-radio class="radio" :label="3">平台管理员</el-radio>
            <el-radio class="radio" :label="4">机构员工</el-radio>
            <el-radio class="radio" :label="5">机构管理员或其他</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false" icon="el-icon-close">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading" icon="el-icon-check">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

export default {
  data() {
    return {

      //start 表格相关========================================================================================
      filters: {
        name: ''
      },
      tableData: [],
      total: 0,
      pageNo: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      listLoading: false,
      sels: [],
      //start 添加相关========================================================================================
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        realName: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //新增界面数据
      addForm: {
        id: null,
        realName: '',
        tel: '',
        email: '',
        inputTime: null,
        state: 1,//状态：0正常，1锁定，2注销
        deptId: null,
        type: 1,//员工类型 ， 1平台普通员工 ，2平台客服人员，3平台管理员，4机构员工，5,机构管理员或其他
        loginId: null
      }

    }
  },
  methods: {
    //start 表格相关============================================================================================
    selsChange: function (sels) {
      this.sels = sels;
    },
    //性别显示转换
    formatState: function (row, column) {
      return row.state == 0 ? '正常' : row.state == 1 ? '锁定' : '注销';
    },
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getTableData();
    },
    //选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getTableData()
    },
    //高级查询相关方法
    keywordQuery() {
      this.pageNo = 1; //从第一页开始查询
      this.getTableData();
    },
    //获取用户列表
    getTableData() {
      this.listLoading = true;
      let para = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,   //当前页需要显示多少条数据
        keyword: this.filters.keyword

      };
      this.$http.post("/system/employee/pagelist", para).then(result => {
        let {success, data, message, code} = result.data;
        if (success) {
          this.total = data.total;
          this.tableData = data.rows;
        } else {
          this.$message({message: "数据加载失败[" + message + "]", type: 'error'});
        }
        this.listLoading = false;
      }).catch(error => {
        this.listLoading = false;
        this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
      })
    },

    //start: 基础CRUD功能=======================================================================================
    //删除
    del: function (row) {
      this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'}).then(() => {
        this.listLoading = true;
        this.$http.delete("/system/employee/" + row.id).then(result => {
          let {success, data, message, code} = result.data;
          if (success) {
            this.$message({message: "提交成功", type: 'success'});
            this.addFormVisible = false;
            this.getTableData();
          } else {
            this.$message({message: "数据加载失败[" + message + "]", type: 'error'});
          }
          this.listLoading = false;
        }).catch(error => {
          this.listLoading = false;
          this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
        })
      });
    },
    //显示编辑界面
    edit: function (row) {
      for (let p in this.addForm) {
        this.addForm[p] = row[p];
      }
      this.addFormVisible = true;
    },
    //显示新增界面
    add: function () {
      this.addFormVisible = true;
      // for(let p in this.addForm){
      // 	console.log(p);
      // 	this.addForm[p] = '';
      // }
      this.addForm = {
        id: null,
        realName: '',
        tel: '',
        email: '520@qq.com',
        inputTime: null,
        state: 0,//状态：0正常，1锁定，2注销
        deptId: null,
        type: 1,//员工类型 ， 1平台普通员工 ，2平台客服人员，3平台管理员，4机构员工，5,机构管理员或其他
        loginId: null
      }
      var user = localStorage.getItem("user");
      var parseUser = JSON.parse(user);
      var logid =parseUser.id
      this.addForm.loginId = logid

      var date = new Date();
      var month = date.getMonth() + 1
      this.addForm.inputTime = date.getFullYear() + '-' + month + '-' + date.getDate()
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.listLoading = true;
            this.$http.post("/system/employee/save", this.addForm).then(result => {
              let {success, data, message, code} = result.data;
              if (success) {
                this.$message({message: "提交成功", type: 'success'});
                this.addFormVisible = false;
                this.getTableData();
              } else {
                this.$message({message: "数据加载失败[" + message + "]", type: 'error'});
              }
              this.listLoading = false;
            }).catch(error => {
              this.listLoading = false;
              this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
            })
          });
        }
      });
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'}).then(() => {
        this.listLoading = true;
        this.$http.patch("/system/employee/patch", ids).then(result => {
          let {success, data, message, code} = result.data;
          if (success) {
            this.$message({message: "提交成功", type: 'success'});
            this.addFormVisible = false;
            this.getTableData();
          } else {
            this.$message({message: "数据加载失败[" + message + "]", type: 'error'});
          }
          this.listLoading = false;
        }).catch(error => {
          this.listLoading = false;
          this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
        })

      })
    }
  },
  mounted() {
    this.getTableData();
  }
}

</script>

<style scoped>

</style>